<%- include('css') %>
<link rel="stylesheet" href="/public/js_module/simditor/css/simditor.css">
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新闻新建</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" placeholder="请输入新闻标题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">来源</label>
            <div class="layui-input-block">
                <input type="text" name="source" lay-verify="required" placeholder="请输入新闻来源" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">封面图</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="headpic">上传图片</button>
                    <div class="layui-upload-list">
                        <img v-if="haspic" style="width: 92px; height: 92px; margin: 0 10px 10px 0;" id="headpreview"
                            v-bind:src="pic">

                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">正文</label>
            <div class="layui-input-block">
                <textarea id="content" name="content">
                </textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">新建</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>

<%- include('script') %>
<!--simditor富文本依赖-->
<script src="/public/js_module/simditor/module.js"></script>
<script src="/public/js_module/simditor/hotkeys.js"></script>
<script src="/public/js_module/simditor/uploader.js"></script>
<script src="/public/js_module/simditor/simditor.js"></script>

<script>

    $(function() {
        var content = new Simditor({
            textarea: $('#content'),
            placeholder: '请输入新闻正文', // 编辑器默认值（默认为’’）
            toolbar: [
                'title',
                'bold',
                'italic',
                'underline',
                'strikethrough',
                'fontScale',
                'color',
                'ol'   ,         // # ordered list
                'ul'   ,         // # unordered list
                'blockquote',
                'code'     ,      //# code block
                'table',
                'link',
                'image',
                'hr'        ,     //# horizontal ruler
                'indent',
                'outdent',
                'alignment'
            ],
            toolbarFloat: true,
            toolbarFloatOffset: 0,
            tabIndent: true,
            upload: {
                url: '/common/upload/zipimg',
                // params: null, //参数
                // fileKey: 'upload_file', //
                // connectionCount: 3,//同时上传数量
                leaveConfirm: 'Uploading is in progress, are you sure to leave this page?' //如果在上传文件时离开页面，将显示消息;
            }
            //optional options
        });
    });

    var vm = new Vue({
        el: '#app',
        data:function(){
            
            return {
                isButtonDisabled:false,
                pic:"",
                haspic:false
            }

        },
        mounted:function(){
            
            var self = this;

            layui.use(['form', 'upload'], function () {
                var form = layui.form;
                var upload = layui.upload;

                //执行实例
                var uploadInst = upload.render({
                    elem: '#headpic' //绑定元素
                        ,
                    url: '/common/upload/zipimg' //上传接口
                        ,
                    before: function (obj) {
                        //预读本地文件示例，不支持ie8
                        obj.preview(function (index, file, result) {
                            self.haspic = true
                            self.pic = result
                        });
                    },
                    done: function (res) {
                        //如果上传失败
                        if (res.code > 0) {
                            self.pic = '';
                            return layer.msg('上传失败');
                        } else {
                            self.pic =  res.url
                        }

                        //上传成功
                    },
                    error: function () {
                        //演示失败状态，并实现重传
                        // var demoText = $('#demoText');
                        // demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                        // demoText.find('.demo-reload').on('click', function(){
                        //     uploadInst.upload();
                        // });
                    }
                });
                //监听提交
                form.on('submit(save)', function (data) {
                    

                    data.field.pic = self.pic//lodash.replace(self.headpic, baseset.server_url, '')
                    self.isButtonDisabled = true;
                    $.post(
                        '/admin/news/add',
                        {data:data.field},
                        function(rs){

                            if (rs.code === '200') {
                                
                                parent.vm.iframesave()
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭  
                                parent.layer.msg('新建成功!')
                                    
                            } else {


                            }
                            self.isButtonDisabled = false;

                        }
                    )



                    return false;
                });
            });
        
        
        },
        
        methods:{

            doclose:function(){
                parent.vm.iframeclose();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引         
                parent.layer.close(index); //再执行关闭 


            }

        }


    })



    
</script>